const pino = require('pino')

const date = new Date();

// 获取当前日期
const dateStr = `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}`;
// 获得当前日期的字符串格式
const timeStr = `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}_${date.getHours()}`;

// 获得当前时间的字符串格式
// 生成日志文件路径
const logPath = `./log/${dateStr}/log_${timeStr}.txt`;
if (!require('fs').existsSync(`./log/${dateStr}`)) {
    require('fs').mkdirSync(`./log/${dateStr}`, { recursive: true });
}

const logger = pino({
    // level: "debug", // 设置记录的最低日志级别
    formatters: {
        bindings(bindings) {
            return { pid: bindings.pid, hostname: bindings.hostname };
        },
    },
    prettyPrint: true,
    timestamp: () => `,"time":"${new Date().toLocaleString()}"`,
    serializers: {
        req: pino.stdSerializers.req, // 标准请求序列化器
        res: pino.stdSerializers.res, // 标准响应序列化器
    },
    ...pino.destination({
        dest: logPath, // omit for stdout
        minLength: 4096, // Buffer before writing
        sync: false, // Asynchronous logging
    }),
});

module.exports = logger;